package com.sky.controller.admin;

import ch.qos.logback.core.joran.util.StringToObjectConverter;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.utils.CacheUtils;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
//import jdk.internal.org.jline.utils.Log;
//import jdk.internal.org.jline.utils.Log;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜品接口
 */
@RestController
@RequestMapping("/admin")
@Api(tags = "菜品相关接口")
@Slf4j
public class DishContoller {
    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    public static final String DISH_CATCHE = "dish_*";

    /**
     * 新增菜品接口
     *
     * @param dto 封装的菜品信息
     * @return
     */
    @PostMapping("/dish")
    @ApiOperation("添加菜品接口")
    public Result saveDish(@RequestBody DishDTO dto) {
        log.info("开始添加菜品{}", dto);
        dishService.saveDishAndFlavor(dto);
        //精准 清空缓存数据
        CacheUtils.cleanDishCacheByCategoryId(redisTemplate,dto.getCategoryId());
        return Result.success();
    }

    @GetMapping("/dish/page")
    @ApiOperation("分页查询接口")
    public Result<PageResult> page(DishPageQueryDTO dto) {
        log.info("开始分页查询{}", dto);
        PageResult pageResult = dishService.page(dto);
        return Result.success(pageResult);
    }

    @DeleteMapping("/dish")
    @ApiOperation("批量删除接口")
    public Result deleteBatch(@RequestParam List<Long> ids) {
        log.info("开始批量删除{}", ids);
        dishService.delete(ids);
        //清空缓存数据
        CacheUtils.cleanDishCacheAll(redisTemplate, DISH_CATCHE);
        return Result.success();
    }

    /**
     * 菜品的启用和禁用
     */
    @PostMapping("/dish/status/{status}")
    @ApiOperation("菜品的停用和启用")
    public Result stopAndStart(@PathVariable Integer status, Long id) {
        log.info("开始菜品禁用和启用{}", status);
        dishService.stopAndStart(status, id);
        //清空缓存数据
        CacheUtils.cleanDishCacheAll(redisTemplate,DISH_CATCHE);
        return Result.success();
    }


    /**
     * 以下菜品的修改
     */
    //菜品回显
    @GetMapping("/dish/{id}")
    @ApiOperation("根据id获取菜品信息")
    public Result<DishVO> getDishAndFlavorByID(@PathVariable Long id) {
        log.info("开始根据id获取菜品信息");
        DishVO dishVO = dishService.getDishFlavorByID(id);
        return Result.success(dishVO);
    }

    //菜品修改
    @PutMapping("/dish")
    @ApiOperation("修改菜品")
    public Result updateDishWithFlavor(@RequestBody DishDTO dishDTO) {
        dishService.updateDishAndFlavor(dishDTO);
        //清空缓存数据
        CacheUtils.cleanDishCacheAll(redisTemplate,DISH_CATCHE);
        return Result.success();
    }


    /**
     * 根据分类id查询菜品
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/dish/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> DishListByCategoryID(@RequestParam Long categoryId) {
        List<Dish> list = dishService.dishListByCategoryID(categoryId);
        return Result.success(list);
    }
}
